import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) {
        // 指定要遍历的文件夹路径
        String folderPath = "C:\\dw-workspace\\agency\\agency-wechat\\pages";

        // 存储文件路径和对应的双大括号内容
        List<String[]> result = new ArrayList<>();

        // 递归遍历文件夹
        traverseFolder(folderPath, result);

        // 输出结果到文件
        outputResultToFile(result, "output.txt");
    }

    public static void traverseFolder(String folderPath, List<String[]> result) {
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 递归遍历子文件夹
                    traverseFolder(file.getAbsolutePath(), result);
                } else if (file.isFile() && file.getName().endsWith(".wxml")) {
                    // 处理 wxml 文件
                    try {
                        extractBraceContent(file.getAbsolutePath(), result);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void extractBraceContent(String filePath, List<String[]> result) throws IOException {
        // 读取文件内容
        String content = new String(Files.readAllBytes(Paths.get(filePath)));

        // 匹配标签属性
        Pattern tagPattern = Pattern.compile("<\\w+\\s+([^<>]*?)>");
        Matcher tagMatcher = tagPattern.matcher(content);

        while (tagMatcher.find()) {
            String attributes = tagMatcher.group(1);

            // 匹配属性值中的双大括号内容
            Pattern pattern = Pattern.compile("(\\w+)=(\"[^\"]*?\"|'[^']*?'|[^\\s>]+)");
            Matcher matcher = pattern.matcher(attributes);

            while (matcher.find()) {
                String attributeValue = matcher.group(2);

                // 检查属性值是否包含双大括号内容
                if (attributeValue.contains("{{")) {
                    String braceContent = attributeValue.replaceAll("\"|'", ""); // 去除双引号或单引号
                    braceContent = braceContent.substring(2, braceContent.length() - 2); // 去除双大括号
                    result.add(new String[]{filePath, braceContent});
                }
            }
        }
    }

    public static void outputResultToFile(List<String[]> result, String outputPath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
            for (String[] entry : result) {
                writer.write("File: " + entry[0]);
                writer.newLine();
                writer.write("Brace Content: " + entry[1]);
                writer.newLine();
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

